package com.juc.learn.deadlock;

import java.util.concurrent.TimeUnit;

/**
 * ClassName:DeadLock
 * Package:com.juc.learn.deadlock
 * Description:
 *
 * @Author:@郭先森
 * @Create:2025/7/27 - 10:33
 * @Version:v1.0
 */
public class DeadLock {
    static Object obj1 = new Object();
    static Object obj2 = new Object();
    public static void main(String[] args) {
      new Thread(()->{
          synchronized (obj1){
              System.out.println(Thread.currentThread().getName()+"持有obj1，试图获取obj2");
              try {
                  TimeUnit.SECONDS.sleep(1);
              } catch (InterruptedException e) {
                  throw new RuntimeException(e);
              }
              synchronized (obj2){
                  System.out.println(Thread.currentThread().getName()+"获取到了obj2");
              }
          }
      },"线程1").start();

        new Thread(()->{
            synchronized (obj2){
                System.out.println(Thread.currentThread().getName()+"持有obj2，试图获取obj1");
                synchronized (obj1){
                    System.out.println(Thread.currentThread().getName()+"获取到了obj1");
                }
            }
        },"线程2").start();

        //验证是否死锁 jps命令获得进程id，  jstack 是jvm自带的堆栈的跟踪信息，可以检测指定进程id是否有死锁
    }
}
